package com.mixsmart.web.plugins.controller;

import java.util.HashMap;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mixsmart.commonweb.plugins.IPlugin;
import com.mixsmart.core.bean.MixResponse;
import com.mixsmart.core.search.filter.SearchFilterFactory;
import com.mixsmart.utils.StringUtils;
import com.mixsmart.web.controller.BaseWebController;
import com.mixsmart.web.plugins.bean.JqGridData;
import com.mixsmart.web.plugins.bean.JqGridSearch;

/**
 * JqGrid插件控制器
 * @author lmq
 * @version 1.0
 * @since JDK版本大于等于1.6
 * 2016年2月2日
 */
@Controller
@RequestMapping("/jqGrid")
public class JqGridController extends BaseWebController {

	@Autowired
	@Qualifier("jqGridPlugin")
	private IPlugin jqGrid;
	@Autowired
	@Qualifier("jqGridTreePlugin")
	private IPlugin jqGridTree;
	
	@RequestMapping("/queryPage/{resName}.do")
	@ResponseBody
	public JqGridData queryPage(@PathVariable String resName, JqGridSearch searchParam, Integer page, Integer rows) throws Exception {
		rows = (null == rows || rows==0)?getPerPageSize():rows;
		page = (null == page || page==0)?1:page;
		JqGridData jgGridData = new JqGridData();
		MixResponse<JqGridData> mixResp = new MixResponse<JqGridData>();
		if(StringUtils.isNotEmpty(resName)) {
			HashMap<String, Object> params = null;
			if(null != searchParam) {
				params = SearchFilterFactory.filterBeanToMap(searchParam);
			}
			mixResp = jqGrid.getDatasPage(resName, params, page, getPerPageSize(rows));
			if(OP_SUCCESS.equals(mixResp.getResult())) {
				jgGridData = mixResp.getData();
			}
		}
		return jgGridData;
	}
	
	@RequestMapping("/query/{resName}.do")
	@ResponseBody
	public JqGridData query(@PathVariable String resName, JqGridSearch searchParam) throws Exception {
		JqGridData jgGridData = new JqGridData();
		MixResponse<JqGridData> mixResp = new MixResponse<JqGridData>();
		if(StringUtils.isNotEmpty(resName)) {
			HashMap<String, Object> params = null;
			if(null != searchParam) {
				params = SearchFilterFactory.filterBeanToMap(searchParam);
			}
			mixResp = jqGrid.getDatas(resName, params);
			if(OP_SUCCESS.equals(mixResp.getResult())) {
				jgGridData = mixResp.getData();
			}
		}
		return jgGridData;
	}
	
	
	@RequestMapping("/queryTree/{resName}.do")
	@ResponseBody
	public JqGridData queryTree(@PathVariable String resName, JqGridSearch searchParam) throws Exception {
		JqGridData jgGridData = new JqGridData();
		MixResponse<JqGridData> mixResp = new MixResponse<JqGridData>();
		if(StringUtils.isNotEmpty(resName)) {
			HashMap<String, Object> params = null;
			if(null != searchParam) {
				params = SearchFilterFactory.filterBeanToMap(searchParam);
			}
			mixResp = jqGridTree.getDatas(resName, params);
			if(OP_SUCCESS.equals(mixResp.getResult())) {
				jgGridData = mixResp.getData();
			}
		}
		return jgGridData;
	}
	
}
